package com.sean.findme.dbentrys;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sean.findme.FmDBHelp;

public class SAXDatabaseHandler extends DefaultHandler{
	
	ArrayList<XMLComm> commList = null;
	XMLComm xmlComm = null;
	
	ArrayList<XMLShopDetail> sdList = null;
	XMLShopDetail xmlShopDetail = null;
	
	ArrayList<XMLSubway> sbList = null;
	XMLSubway xmlSubway = null;
	
	ArrayList<XMLSubwayDetail> sbdList = null;
	XMLSubwayDetail xmlSubwayDetail = null;
	
	String curDatabase = null;
	String curTable = null;
	String curTag = null;
	
	boolean bFinished = false;
	
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String data = new String(ch,start,length);
		if( "shop_detail_tb".equals(curTable) && (xmlShopDetail != null) )
		{
			xmlShopDetail.setDatabase(curDatabase);
			xmlShopDetail.setTable(curTable);
			if( "shop_key".equals(curTag) )
				xmlShopDetail.setShop_key(data);
			else if( "city_key".equals(curTag) )
				xmlShopDetail.setCity_key(data);
			else if( "name".equals(curTag) )
				xmlShopDetail.setName(data);
			else if( "address".equals(curTag) )
				xmlShopDetail.setAddress(data);
			else if( "open".equals(curTag) )
				xmlShopDetail.setOpening_time(data);
			else if( "phone".equals(curTag) )
				xmlShopDetail.setPhone(data);
			else if( "position".equals(curTag) )
				xmlShopDetail.setPosition(data);
			else if( "favorable".equals(curTag) )
				xmlShopDetail.setFavorable(data);
				
		}
		else if( "subway_tb".equals(curTable) && (xmlSubway != null) )
		{
			xmlSubway.setDatabase(curDatabase);
			xmlSubway.setTable(curTable);
			if( "subway_key".equals(curTag) )
				xmlSubway.setSubway_key(data);
			else if( "city_key".equals(curTag) )
				xmlSubway.setCity_key(data);
			else if( "name".equals(curTag) )
				xmlSubway.setName(data);
		}
		else if( "subway_detail_tb".equals(curTable) && (xmlSubwayDetail != null) )
		{
			xmlSubwayDetail.setDatabase(curDatabase);
			xmlSubwayDetail.setTable(curTable);
			if( "subway_key".equals(curTag) )
				xmlSubwayDetail.setSubway_key(data);
			else if( "city_key".equals(curTag) )
				xmlSubwayDetail.setCity_key(data);
			else if( "name".equals(curTag) )
				xmlSubwayDetail.setName(data);
			else if( "position".equals(curTag) )
				xmlSubwayDetail.setPosition(data);
		}
		else if( xmlComm != null )
		{
			xmlComm.setDatabase(curDatabase);
			xmlComm.setTable(curTable);
			if( "shop_tb".equals(curTable) || "city_tb".equals(curTable) )
			{
				if( "key".equals(curTag) )
					xmlComm.setUnknown1(data);
				else if( "name".equals(curTag) )
					xmlComm.setUnknown2(data);
			}
			else if( "trade_zone_tb".equals(curTable) )
			{
				if( "name".equals(curTag) )
					xmlComm.setUnknown1(data);
				else if( "position".equals(curTag) )
					xmlComm.setUnknown2(data);
				else if("key".equals(curTag))
					xmlComm.setUnKnown3(data);
					
			}
			else if( "me_enjoy_tb".equals(curTable) || "her_enjoy_tb".equals(curTable) || "favorable_tb".equals(curTable) )
			{
				if( "key".equals(curTag) )
					xmlComm.setUnknown1(data);
			}
		}
				
		super.characters(ch, start, length);
	}
	@Override
	public void endDocument() throws SAXException {
		bFinished = true;
		super.endDocument();
	}
	@Override
	public void startDocument() throws SAXException {
		
		bFinished = false;
		super.startDocument();
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if( "database".equals(localName) )
		{
			curDatabase = attributes.getValue("", "id");
			if( "1".equals(curDatabase) )
				commList = new ArrayList<XMLComm>();
			else if( "2".equals(curDatabase) )
				sdList = new ArrayList<XMLShopDetail>();
			else if( "3".equals(curDatabase) )
				sbList = new ArrayList<XMLSubway>();
			else if( "4".equals(curDatabase) )
				sbdList = new ArrayList<XMLSubwayDetail>();
		}
		else if( "table".equals(localName) )
		{
			curTable = attributes.getValue("", "name");	
		}
		else if( "value".equals(localName) )
		{
			if( "shop_detail_tb".equals(curTable) )
				xmlShopDetail = new XMLShopDetail();
			else if( "subway_tb".equals(curTable) )
				xmlSubway = new XMLSubway();
			else if( "subway_detail_tb".equals(curTable) )
				xmlSubwayDetail = new XMLSubwayDetail();
			else if( "shop_tb".equals(curTable) || "city_tb".equals(curTable) || "trade_zone_tb".equals(curTable) 
					|| "me_enjoy_tb".equals(curTable) || "her_enjoy_tb".equals(curTable) || "favorable_tb".equals(curTable))
				xmlComm = new XMLComm();
		}
		else
			curTag = localName;
			
		super.startElement(uri, localName, qName, attributes);
	}
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if( "database".equals(localName) )
		{
			curDatabase = null;
		}
		else if( "table".equals(localName) )
		{
			curTable = null;
			
		}
		else if( "value".equals(localName) )
		{
			if( "shop_detail_tb".equals(curTable) )
			{
				sdList.add(xmlShopDetail);
				xmlShopDetail = null;
			}
			else if( "subway_tb".equals(curTable) )
			{
				sbList.add(xmlSubway);
				xmlSubway = null;
			}
			else if( "subway_detail_tb".equals(curTable) )
			{
				sbdList.add(xmlSubwayDetail);
				xmlSubwayDetail = null;
			}
			else if( "shop_tb".equals(curTable) || "city_tb".equals(curTable) || "trade_zone_tb".equals(curTable) 
					|| "me_enjoy_tb".equals(curTable) || "her_enjoy_tb".equals(curTable) || "favorable_tb".equals(curTable))
			{
				commList.add(xmlComm);
				xmlComm = null;
			}
		}
		else
			curTag = null;
		super.endElement(uri, localName, qName);
	}
	
	
	public ArrayList<XMLComm> getCommList() {
		return commList;
	}
	public void setCommList(ArrayList<XMLComm> commList) {
		this.commList = commList;
	}
	public ArrayList<XMLShopDetail> getSdList() {
		return sdList;
	}
	public void setSdList(ArrayList<XMLShopDetail> sdList) {
		this.sdList = sdList;
	}
	public ArrayList<XMLSubway> getSbList() {
		return sbList;
	}
	public void setSbList(ArrayList<XMLSubway> sbList) {
		this.sbList = sbList;
	}
	public ArrayList<XMLSubwayDetail> getSbdList() {
		return sbdList;
	}
	public void setSbdList(ArrayList<XMLSubwayDetail> sbdList) {
		this.sbdList = sbdList;
	}
	public boolean isbFinished() {
		return bFinished;
	}
	public void setbFinished(boolean bFinished) {
		this.bFinished = bFinished;
	}
	
	public void displayAll()
	{
		if( commList != null )
		{
			for( int i = 0; i < commList.size(); i++ )
			{
				FmDBHelp.getInstance(null, 0).commDBEntry.insertOneRecord(commList.get(i));
			}
		}
		
		if( sdList != null )
		{
			for( int i = 0; i < sdList.size(); i++ )
				FmDBHelp.getInstance(null, 0).shopDetailDBEntry.insertOneRecord(sdList.get(i));
		}
		
		if( sbdList != null )
		{
			for( int i = 0; i < sbdList.size(); i++ )
				FmDBHelp.getInstance(null, 0).subwayDetailDBEntry.insertOneRecord(sbdList.get(i));
		}
		
		if( sbList != null )
			for( int i = 0; i < sbList.size(); i++ )
				FmDBHelp.getInstance(null, 0).subwayDBEntry.insertOneRecord(sbList.get(i));
			
	}
	
	

}
